package com.lemon.api.auto.cases;

import com.lemon.api.auto.ApiExecutor.ApiExecutor;
import com.lemon.api.auto.util.*;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.*;

@Test(enabled = false)
public class RegisterCase {
    public void doGet(String url,List<NameValuePair>parameters,String charset){
        String parameterString = URLEncodedUtils.format(parameters, charset);
        url +=("?"+parameterString);
        //接口url地址
/*        String url="http://test.lemonban.com/futureloan/mvc/api/member/login";
        String mobilephone="13517315120";
        String pwd="123456";
        url+=("?mobilephone="+mobilephone+"+&pwd="+pwd);*/
        //创建请求对象
        HttpGet httpGet = new HttpGet(url);
        //创建http客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //添加请求头指定数据格式
        httpGet.addHeader("Content-Type","application/x-www-form-urlencoded;charest=UTF-8");
        //发包
        try {
            //调用完接口获取到服务器的响应
            HttpResponse httpResponse = httpClient.execute(httpGet);
            //从响应对象中取出状态码
            int code = httpResponse.getStatusLine().getStatusCode();
            //从响应对象中取出响应报文
            String result = EntityUtils.toString(httpResponse.getEntity());
            //从响应对象中取出响应头
            String responseHeadersString = Arrays.toString(httpResponse.getAllHeaders());
            System.out.println("状态码:["+code+"]");
            System.out.println("响应报文:["+result+"]");
            System.out.println("响应头:["+responseHeadersString+"]");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    @Test(enabled = false)
    public static void doPost(String url, List<NameValuePair>parameters,String charset){
        //创建请求对象
        HttpPost httpPost = new HttpPost(url);
        //创建http客户端对象
        HttpClient httpClient = HttpClients.createDefault();
        //发包
        try {
//            List<BasicNameValuePair> paramters = new ArrayList<BasicNameValuePair>();
            //设置请求头
            httpPost.addHeader(new BasicHeader("Content-Type","application/x-www-form-urlencoded;charest=UTF-8"));
            //准备参数，将参数设置到请求体中
            //UrlEncodedFormEntity:以表单的方式传递数据（mobilephone=13517315120&pwd=123456）
            String parameterString = URLEncodedUtils.format(parameters, charset);
            httpPost.setEntity(new StringEntity(parameterString,charset));
            //调用完接口获取到服务器的响应
            HttpResponse httpResponse = httpClient.execute(httpPost);
            //从响应对象中取出状态码
            int code = httpResponse.getStatusLine().getStatusCode();
            //从响应对象中取出响应报文
            String result = EntityUtils.toString(httpResponse.getEntity());
            //从响应对象中取出响应头
            String responseHeadersString = Arrays.toString(httpResponse.getAllHeaders());
            System.out.println("状态码:["+code+"]");
            System.out.println("响应报文:["+result+"]");
            System.out.println("响应头:["+responseHeadersString+"]");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
@Test(dataProvider = "dl")
    public void test(String url,String type,String paramString) {
    ApiExecutor.doService(url, type, paramString);
        /*        if("post".equalsIgnoreCase(type)){
            if ("form".equalsIgnoreCase(contentType)){

            }else {
                JsonSubmitApiExecutor.doPost(url,paramters,charset);
            }

        }else if ("get".equalsIgnoreCase(type)){
            if ("form".equalsIgnoreCase(contentType)){

            }else {
                JsonSubmitApiExecutor.doGet(url,paramters,charset);
            }
        }*/
    }




    @DataProvider(name = "dl")
    public Object [][] datas(){
        int [] rows={2,3,4,5,6,7};
        int [] cells ={3,4,6};
        String filepath="src/test/resources/cases_v3.xls";
        String [] cellName={"ApiId","Params"};
        Object[][] datas = CaseUtil.datas(cellName);

        return datas;
    }

/*    public static void main(String[] args) {
        String jsonString="{\"mobilephone\":\"19000000000\",\"pwd\":\"\"}\n";
        Map<String,String> jsonMap = (Map<String, String>) JSONObject.parse(jsonString);
        System.out.println(jsonMap.get("mobilephone"));
    }*/

}
